﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using ElectronicCommerce.Framework.Data.Utility.EntityQueryConditionBuilder;

namespace ElectronicCommerce.Framework.Data.Entity.EntityQueryCondition
{

    [DataContract]
    public class SortCondition<T> : PageQueryCondition
    {
        /// <summary>
        /// Condition
        /// </summary>
        [DataMember]
        public string Condition { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="SortCondition"/> class.
        /// </summary>
        public SortCondition()
        {
            //this.SortList = new List<Sort<T>>();
        }

        /// <summary>
        /// 多字段排序。
        /// </summary>
        [DataMember]
        public List<Sort<T>> SortList { get; set; }


        public override string ToString()
        {
            if (SortList != null && SortList.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var sort in SortList)
                {
                    sb.AppendFormat(" {0} ,", sort.ToString());
                }
                return sb.ToString().TrimEnd(',');
            }
            return string.Empty;
        }
    }

    [DataContract]
    public class Sort<T>
    {
        /// <summary>
        /// 获取或设置排序字段
        /// </summary>
        [DataMember]
        public T SortField { get; set; }
        /// <summary>
        /// 获取或设置排序类型
        /// </summary>
        [DataMember]
        public SortDirection SortDirection { get; set; }

        /// <summary>
        /// 获取排序类型
        /// </summary>
        [DataMember]
        public int SortDirectionIntValue { get; set; }

        /// <summary>
        /// 别名 多表查询的时候建议设置.
        /// </summary>
        [DataMember]
        public string Alias { get; set; }

        /// <summary>
        /// 添加排序字段, 默认升序.
        /// </summary>
        /// <param name="sortField">排序字段</param>
        public void Add(T sortField)
        {
            Add(sortField, SortDirection.Asc);
        }

        /// <summary>
        /// 添加排序字段, 和排序类型
        /// </summary>
        /// <param name="sortField">排序字段</param>
        /// <param name="sortDirection">排序类型</param>
        public void Add(T sortField, SortDirection sortDirection)
        {
            SortField = sortField;
            SortDirection = sortDirection;
            SortDirectionIntValue = (int)sortDirection;
        }

        /// <summary>
        /// 返回排序字符串  例如 ORDER BY id Descending
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (SortField.ToString() != "UnSort")
            {
                StringBuilder sbSort = new StringBuilder();
                if (!string.IsNullOrEmpty(Alias))
                {
                    sbSort.Append(Alias);
                    sbSort.Append(".");
                }
                sbSort.Append(SortField.ToString());
                sbSort.Append(" ");
                sbSort.Append(SortDirection.ToString());
                return sbSort.ToString();
            }
            else
                return string.Empty;
        }
    }
}
